package com.strange.mdl.module.mdl.service.activity;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.strange.mdl.module.mdl.controller.app.activity.vo.AppActivityRespVO;
import com.strange.mdl.module.mdl.controller.app.activity.vo.AppActivityUserRespVO;
import com.strange.mdl.module.mdl.dal.dataobject.activity.ActivityUserDO;
import com.strange.mdl.module.mdl.dal.mysql.activity.ActivityUserMapper;
import com.strange.mdl.module.mdl.utils.ConvertUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.strange.mdl.module.mdl.controller.admin.activity.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.activity.ActivityDO;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import com.strange.mdl.module.mdl.dal.mysql.activity.ActivityMapper;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 活动 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityMapper activityMapper;


    @Resource
    private ActivityUserMapper activityUserMapper;

    @Override
    public Long createActivity(ActivitySaveReqVO createReqVO) {
        // 插入
        ActivityDO activity = BeanUtils.toBean(createReqVO, ActivityDO.class);
        activityMapper.insert(activity);
        // 返回
        return activity.getId();
    }

    @Override
    public void updateActivity(ActivitySaveReqVO updateReqVO) {
        // 校验存在
        validateActivityExists(updateReqVO.getId());
        // 更新
        ActivityDO updateObj = BeanUtils.toBean(updateReqVO, ActivityDO.class);
        activityMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivity(Long id) {
        // 校验存在
        validateActivityExists(id);
        // 删除
        activityMapper.deleteById(id);
    }

    @Override
        public void deleteActivityListByIds(List<Long> ids) {
        // 校验存在
        validateActivityExists(ids);
        // 删除
        activityMapper.deleteByIds(ids);
        }

    private void validateActivityExists(List<Long> ids) {
        List<ActivityDO> list = activityMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ACTIVITY_NOT_EXISTS);
        }
    }

    private void validateActivityExists(Long id) {
        if (activityMapper.selectById(id) == null) {
            throw exception(ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public ActivityDO getActivity(Long id) {
        return activityMapper.selectById(id);
    }

    @Override
    public AppActivityRespVO getAppActivity(Long id, Long userId) {

        ActivityDO activityDO = activityMapper.selectById(id);
        AppActivityRespVO bean = BeanUtils.toBean(activityDO, AppActivityRespVO.class);
        if (activityDO != null) {
            LambdaQueryWrapper<ActivityUserDO> auWrapper = new LambdaQueryWrapper<>();
            auWrapper.eq(ActivityUserDO::getActivityId, bean.getId());
            auWrapper.notIn(ActivityUserDO::getStatus, List.of(1, 4)); // 状态为1、4的，不显示
            List<ActivityUserDO> activityUserLists = activityUserMapper.selectList(auWrapper);
            List<AppActivityUserRespVO> bean1 = BeanUtils.toBean(activityUserLists, AppActivityUserRespVO.class);
            bean.setUserLists(bean1);
            Integer i = 0;
            for (AppActivityUserRespVO appActivityUserRespVO : bean1) {
                if (List.of(0,2,3).contains(appActivityUserRespVO.getStatus())) {
                    i++;
                }
            }
            bean.setUserNum(i);
        }
        return bean;
    }

    @Override
    public PageResult<ActivityDO> getActivityPage(ActivityPageReqVO pageReqVO) {

        LambdaQueryWrapper<ActivityDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageReqVO.getTitle() != null, ActivityDO::getTitle, pageReqVO.getTitle());
//        List<SortingField> sortingFields = List.of(new SortingField().setField("id").setOrder("desc"));
        return activityMapper.selectPage(pageReqVO, null, wrapper);
    }


    @Override
    public PageResult<AppActivityRespVO> getAppActivityPage(ActivityPageReqVO pageReqVO) {

        LambdaQueryWrapper<ActivityDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageReqVO.getTitle() != null, ActivityDO::getTitle, pageReqVO.getTitle());
        PageResult<ActivityDO> activityDOPageResult = activityMapper.selectPage(pageReqVO, null, wrapper);
        PageResult<AppActivityRespVO> result = BeanUtils.toBean(activityDOPageResult, AppActivityRespVO.class);
        for (AppActivityRespVO appActivityRespVO : result.getList()) {
            LambdaQueryWrapper<ActivityUserDO> auWrapper = new LambdaQueryWrapper<>();
            auWrapper.eq(ActivityUserDO::getActivityId, appActivityRespVO.getId());
            auWrapper.notIn(ActivityUserDO::getStatus, List.of(1, 4)); // 状态为1、4的，不显示
            List<ActivityUserDO> activityUserLists = activityUserMapper.selectList(auWrapper);
            appActivityRespVO.setUserLists(ConvertUtils.sourceToTarget(activityUserLists, AppActivityUserRespVO.class));
        }
        return result;
    }



}